﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Detai.Admin.Models;
using Detai.Admin.ViewModels;
using Detai.Admin.Components;
namespace Detai.Admin.Controllers
{
    public class GiangvienController : Controller
    {
        private bool isRoled = SystemConstants.Kiemtraquyen(1);
       
        DetaiDataContext eDataContext = new DetaiDataContext();

        [Authorize()]
        public ActionResult Index()
        {
            if (!isRoled) { return RedirectToAction("Index", "Thongbao", new { message = SystemConstants.Khongcoquyen }); }
            GiangvienIndexViewModel model = new GiangvienIndexViewModel();
            model.Giangviens = eDataContext.Giangviens.ToList();
            return View(model);
        }

        [Authorize()]
        public ActionResult Create()
        {
            if (!isRoled) { return RedirectToAction("Index", "Thongbao", new { message = SystemConstants.Khongcoquyen }); }
            return View();
        }


        [HttpPost]
        [Authorize()]
            [ValidateInput(false)]
        public ActionResult Create(Giangvien formData)
        {
            try
            {
                UpdateModel(formData);
                eDataContext.Giangviens.InsertOnSubmit(formData);
                eDataContext.SubmitChanges();
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        [Authorize()]
        public ActionResult Edit(int id)
        {
            var model = eDataContext.Giangviens.FirstOrDefault(e => e.MaGV == id);
            return View(model);
        }

        [HttpPost]
        [Authorize()]
        [ValidateInput(false)]
        public ActionResult Edit(int id, Giangvien formData)
        {
            try
            {
                formData = eDataContext.Giangviens.FirstOrDefault(e => e.MaGV == id);
                UpdateModel(formData);
                eDataContext.SubmitChanges();
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        [Authorize()]
        public ActionResult Delete(int id)
        {
            var item = eDataContext.Giangviens.FirstOrDefault(e => e.MaGV == id);
            return View(item);
        }

        [HttpPost]
        [Authorize()]
        public ActionResult Delete(int id, Giangvien fromdata)
        {
            try
            {
                var item = eDataContext.Giangviens.FirstOrDefault(e => e.MaGV == id);
                if (item != null)
                {
                    // Xóa tài khoản đăng nhập
                    var itemtk = eDataContext.TaiKhoans.FirstOrDefault(e => e.MaTK == id);
                    if (itemtk != null)
                    {
                        eDataContext.TaiKhoans.DeleteOnSubmit(itemtk);
                        eDataContext.SubmitChanges();
                    }
                    // Xóa tài thông tin giảng viên
                    eDataContext.Giangviens.DeleteOnSubmit(item);
                    eDataContext.SubmitChanges();
                }
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

    }
}
